package org.datacoder.geocoding.datastructures.intervals

import org.datacoder.geocoding.*
import org.datacoder.geocoding.datastructures.geoshapes.*
import org.datacoder.geocoding.jurisdiction.*

class IntervalsSequentialScan implements IntersectingIntervalsFinder {

    GeoInterval[] geoIntervals
    int type
    int length

    public IntervalsSequentialScan(Collection<Jurisdiction> jurisdictions, int type){
        this.type = type
        length = jurisdictions.size()
        geoIntervals = new GeoInterval[length]
        int i = 0
        for(juris in jurisdictions){
            geoIntervals[i++] = juris.toGeoInterval(type)
        }
    }

    public Set<Jurisdiction> findIntersectingIntervals(GeoInterval gi){
        Set<Jurisdiction> jurisdictions = new HashSet<Jurisdiction>()
        for(int i = 0; i < length; i++){
            if(gi.intersectsPerpendicularInterval(geoIntervals[i])){
                jurisdictions.add(geoIntervals[i].juris)
            }
        }
        return jurisdictions
    }

}

